/**
 * 作者:hanbo
 * 日期:2018/12/2710:09
 * 描述:
 */
define(["dojo/_base/declare",
    "esri/widgets/DistanceMeasurement2D",
    "esri/widgets/AreaMeasurement2D",
    "esri/widgets/LayerList",
    "esri/widgets/Home",
    "esri/Viewpoint",
    "esri/geometry/Extent",
    "esri/widgets/ScaleBar",
    "esri/widgets/Print",
    "dojo/on",
    "dojo/dom"], function (declare, DistanceMeasurement2D,AreaMeasurement2D,LayerList, Home, Viewpoint, Extent, ScaleBar,Print,on,dom) {
    return declare("UIControl", null, {
        constructor: function (view) {
            this.view = view;

            this.tempWidget=null;
            this.layerList=null;

            //移除底部文字
            view.ui.remove("attribution");
            var homeButton = new Home({
                view: view,
                viewpoint: new Viewpoint({
                    targetGeometry: new Extent({
                        xmax:  109.908090,
                        ymax:  35.174175,
                        xmin: 107.739930,
                        ymin: 34.173820 ,
                        spatialReference: {
                            wkid: 4326
                        }
                    })
                })
            });
            view.ui.add(homeButton, "top-left");
            view.ui.add("topbar", "top-left");
            view.ui.add("rightbar", "top-right");
            view.ui.add("baseMapDiv", "top-right");
            this.initMeasure(view);
            this.initListManager(view);
            this.initBaseMap(view);
        },
        setScaleBar: function (bool) {
            if (bool) {
                this.scaleBar = new ScaleBar({
                    view: this.view,
                    unit: "metric"
                });
                this.view.ui.add(this.scaleBar, {
                    position: "bottom-left"
                });
            } else if (this.scaleBar) {
                this.view.ui.remove(this.scaleBar);
                this.scaleBar = null;
            }
        },
        initListManager:function(view){
            var that=this;
            document.getElementById("layerButton").addEventListener("click",
                function () {
                    if (!this.classList.contains('active')) {
                        this.classList.add("active");
                        that.layerList = new LayerList({
                            view: view
                        });
                        // 图层控件
                        view.ui.add(that.layerList, "top-right");
                    } else {
                        document.getElementById("layerButton").classList.remove("active");
                        view.ui.remove(that.layerList);
                        that.layerList.destroy();
                        that.layerList = null;
                    }
                }
            );
        },
        initMeasure:function (view) {
             var activeWidget=this.tempWidget;
            document.getElementById("distanceButton").addEventListener("click",
                function () {
                    setActiveWidget(null);
                    if (!this.classList.contains('active')) {
                        setActiveWidget('distance');
                    } else {
                        setActiveButton(null);
                    }
                });

            document.getElementById("areaButton").addEventListener("click",
                function () {
                    setActiveWidget(null);
                    if (!this.classList.contains('active')) {
                        setActiveWidget('area');
                    } else {
                        setActiveButton(null);
                    }
                });

            function setActiveWidget(type) {
                switch (type) {
                    case "distance":
                        activeWidget = new DistanceMeasurement2D({
                            view: view
                        });
                        activeWidget.viewModel.newMeasurement();
                        view.ui.add(activeWidget, "top-right");
                        setActiveButton(document.getElementById('distanceButton'));
                        break;
                    case "area":
                        activeWidget = new AreaMeasurement2D({
                            view: view
                        });
                        activeWidget.viewModel.newMeasurement();
                        view.ui.add(activeWidget, "top-right");
                        setActiveButton(document.getElementById('areaButton'));
                        break;
                    case null:
                        if (activeWidget) {
                            view.ui.remove(activeWidget);
                            activeWidget.destroy();
                            activeWidget = null;
                        }
                        break;
                }
            }

            function setActiveButton(selectedButton) {
                view.focus();
                document.getElementById('distanceButton').classList.remove("active");
                document.getElementById('areaButton').classList.remove("active");
                if (selectedButton) {
                    selectedButton.classList.add("active");
                }
            }
        },
        initBaseMap:function (view) {
            var that=this;
            document.getElementById("baseMapButton").addEventListener("click",
                function () {
                    if (!this.classList.contains('active')) {
                        this.classList.add("active");
                        document.getElementById("baseMapDiv").style.display="block"

                    } else {
                        document.getElementById("baseMapButton").classList.remove("active");
                        document.getElementById("baseMapDiv").style.display="none"
                    }
                }
            );
        }
    })
});